"""
https://www.python-engineer.com/courses/advancedpython/11-json/
"""
import json

# 1. Python to json
# 1.1 case1
person = {"name": "John", "age": 30, "city": "New York", "hasChildren": False, "titles": ["engineer", "programmer"]}

# convert into JSON:
person_json = json.dumps(person)
# use different formatting style
person_json2 = json.dumps(person, indent=4, separators=("; ", "= "), sort_keys=True)

# the result is a JSON string:
print(person_json)
print(person_json2)

# 1.2 case2
person = {"name": "John", "age": 30, "city": "New York", "hasChildren": False, "titles": ["engineer", "programmer"]}

with open('person.json', 'w') as f:
    json.dump(person, f)  # you can also specify indent etc...

# 2. Json to python
# 2.1 load data from dictionary
person_json = """
{
    "age": 30, 
    "city": "New York",
    "hasChildren": false, 
    "name": "John",
    "titles": [
        "engineer",
        "programmer"
    ]
}
"""
person = json.loads(person_json)
print(person)

# 2.2 load data from file
with open('person.json', 'r') as f:
    person = json.load(f)
    print(person)
